package controller

import (
	. "gddgame.cc/galaxy/common/dsl/web"
	"gddgame.cc/galaxy/common/namespaces/cooperate/structure"
	"gddgame.cc/galaxy/project/breeder/breeder/cooperated"
	"gddgame.cc/galaxy/project/breeder/breeder/model"
	"gddgame.cc/galaxy/utils/def"
)

func UpgradeScience(context def.Context, scienceId int, user *model.User, account *cooperated.Account) (map[structure.PublicDataType]map[int]*model.Effect, error) {
	scienceInfo, ok := Center.ScienceMap[scienceId]
	if !ok {
		return nil, ValidException("科技数据错误")
	}
	level, ok := user.Sciences[scienceId]
	if ok {
		if level+1 == len(scienceInfo.Levels) {
			return nil, ValidException("科技已经满级")
		}
		level += 1
	} else {
		level = 1
	}
	levelInfo := scienceInfo.Levels[level-1]
	user.Sciences[scienceId] = level
	if !ValidRequire(context, levelInfo.RequireSlice, user, account, nil, nil) {
		return nil, ValidException("不满足升级条件")
	}
	if ok, consumeData := ValidConsume(context, levelInfo.ConsumeSlice, 1, user, nil); !ok {
		return nil, ValidException("升级所需不足")
	} else {
		if consumeData.UserData == nil {
			consumeData.UserData = map[string]interface{}{}
		}
		// 一起更新
		consumeData.UserData["sciences"] = user.Sciences
		if err := UpdateConsume(context, consumeData, user, nil); err != nil {
			return nil, err
		}

	}
	return levelInfo.TargetEffectSlice, nil
}
